package tpi.gui.panels.info

import dominio.ubicaciones.Pais
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.form.DropDownChoice
import org.apache.wicket.markup.html.form.Form
import org.apache.wicket.markup.html.panel.Panel
import org.apache.wicket.model.CompoundPropertyModel
import org.apache.wicket.model.PropertyModel
import org.uqbar.commons.model.UserException
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods
import org.uqbar.wicket.xtend.XButton
import org.uqbar.wicket.xtend.XListView

class ConexionesPanel extends Panel {
	
	extension WicketExtensionFactoryMethods = new WicketExtensionFactoryMethods
	private final Pais pais
	private Pais paisSeleccionado
	
	new(String id, Pais pais) {
		super(id)
		this.pais = pais
		this.defaultModel = pais.asCompoundModel
		val Form<Pais> form = new Form<Pais>("conexionesForm", new CompoundPropertyModel(pais))
		val listView = new XListView("conexiones")
		listView.populateItem = [c |
			c.model = c.modelObject.asCompoundModel
			c.addChild(new Label("nombre"))
			c.addChild(new XButton("eliminar").onClick = [| eliminar(c.modelObject)])
		]
		form.addChild(listView)
		form.addChild(new DropDownChoice("conexionesPosibles",
             new PropertyModel(this, "paisSeleccionado"),
             pais.mapamundi.paises))
        form.addChild(new XButton("agregarConexion").onClick = [| 
			agregarConexionSeleccionada
		])
		addChild(form)
	}
	
	def agregarConexionSeleccionada() {
		if(paisSeleccionado == null)
			throw new UserException("Debe elejir un pais!")
		pais.agregarConexion(paisSeleccionado)
		paisSeleccionado = null
	}
	
	def eliminar(Pais conexion) {
		pais.borrarConexion(conexion)
	}
	
}